<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\WechatReply;
use App\Models\WechatKeywordSet;
use Illuminate\Support\Facades\DB;
use App\Utils\FileUploader;
use Illuminate\Support\Facades\Redis;

class WechatReplyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

                  
        $wechat = new WechatReply();
       
        $data = $wechat ->orderBy("event","asc")->orderBy("id","desc")->paginate(15);


        return view("WechatReply.index",['data'=>$data]);
    }

    /*
     * 消息回复列表
     */
    public function setList(Request $request){
        $keywordList = WechatKeywordSet::paginate(10);
        return view('WechatReply.list',['keywordList'=>$keywordList]);
    }

    /*
     * 添加关键词页面
     */
    public function keywordAdd(Request $request){


        return view('WechatReply.keywordAdd');
    }

    /*
     * 添加关键词编辑页面
     */
    public function keywordEdit(Request $request){
        $id = $request->input('id');
        $keywordSet = WechatKeywordSet::where('id',$id)->first();
        return view('WechatReply.keywordEdit',['keywordSet'=>$keywordSet]);
    }

    /*
     * 创建回复
     */
    public function postKeywordSet(Request $request){

        $upload  = new FileUploader();
        $arr =  $upload->upload($request,'img_url');
        //dd($arr);
        $img_url = $arr['url'];
        $data = $this->uploadMedia($img_url);
        dd($data);
        $keyword = $request->input('keyword');
        $content = $request->input('content');
        $contents = explode(PHP_EOL,$content);
        $reply = '';
        for($i = 0;$i< count($contents);$i++){
            if(empty($contents[$i])){
                continue;
            }
            if($i == count($contents)-1){
                $reply .= trim($contents[$i]);
            }else{
                $reply .= trim($contents[$i])."\n";
            }
        }

        $keywordSet = new WechatKeywordSet();
        $keywordSet->keyword = $keyword;
        $keywordSet->reply_content = $reply;
        $keywordSet->img_url = $img_url;

        if($keywordSet->save()){
            $this->getSessionInfo($request,'success','添加成功');
            return redirect('/wechatSet/list');
        }else{
            $this->getSessionInfo($request,'fail','添加失败');
            return redirect('/wechatSet/keywordAdd');
        }
    }

    /*
     *更新关键词设置
     */
    public function updateKeywordSet(Request $request){
        $id = $request->input('id');
        $keyword = $request->input('keyword');
        $content = $request->input('content');
        $contents = explode(PHP_EOL,$content);
        $reply = '';
        for($i = 0;$i< count($contents);$i++){
            if(empty($contents[$i])){
                continue;
            }
            if($i == count($contents)-1){
                $reply .= trim($contents[$i]);
            }else{
                $reply .= trim($contents[$i])."\n";
            }
        }
        $keywordSet = WechatKeywordSet::where('id',$id)->first();
        $keywordSet->keyword = $keyword;
        $keywordSet->reply_content = $reply;
        if($keywordSet->save()){
            $this->getSessionInfo($request,'success','修改成功');
            return redirect('/wechatSet/list');
        }else{
            $this->getSessionInfo($request,'fail','修改失败');
            return redirect('/wechatSet/keywordAdd');
        }
    }

    /*
     * 删除关键词
     */

    public function keywordDelete(Request $request){
        $id = $request->input('id');
        $keywordSet = WechatKeywordSet::where('id',$id)->delete();
        return $this->getMessage(0,'删除成功');
    }

    /*
     * 修改状态
     */

    public function updateState(Request $request){
        $id = $request->input('id');
        $state = $request->input('state');
        $id = $request->input('id');
        $keywordSet = WechatKeywordSet::where('id',$id)->first();
        $keywordSet->state = $state;
        if($keywordSet->save()){
            return $this->getMessage(0,'修改成功');
        }else{
            return $this->getMessage(1,'修改失败');
        }
    }

    /**
     * 添加信息
     */
    public function add()
    {

        return view("WechatReply.add");
    }


    public function update(Request $request){
        
        $id = $request->input("id");
        $data = WechatReply::where("id",$id)->get();
       // dd($data);
        return view("WechatReply.update",['data'=>$data,'id'=>$id]);
    }

    public function store(Request $request){
        $data = $request->input();
        array_shift($data);
        //dd($data);
        $wechat = new WechatReply();
        $wechat->title = $data['title'];
        $wechat->event = $data['event'];
        $wechat->keyword = $data['keyword'];
        $wechat->type = $data['type'];
        $wechat->content = $data['content'];
        
        $re = $wechat->save();
        if($re){
            return redirect("wechatReply/index");
        }
        //dd($data);
    } 
     public function updateData(Request $request){
        $data = $request->input();
        array_shift($data);
        $id = $data['id'];
        array_shift($data);
        $re = WechatReply::where("id",$id)->update($data);
        if($re){
            return redirect("wechatReply/index");
        }
        //dd($data);
    } 
    public function del(Request $request){
        $id = $request->input("id");
        
        $re = WechatReply::where("id",$id)->delete($id);
        if($re){
            return redirect("wechatReply/index");
        }
    }



   
}
